import java.awt.Component;
import java.awt.Container;
import java.awt.GridLayout;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class XAxisAlignY {
  private static Container makeIt(String title, float alignment) {
    String labels[] = { "--", "--", "--" };

    JPanel container = new JPanel();
    container.setBorder(BorderFactory.createTitledBorder(title));
    BoxLayout layout = new BoxLayout(container, BoxLayout.X_AXIS);
    container.setLayout(layout);

    for (int i = 0, n = labels.length; i < n; i++) {
      JButton button = new JButton(labels[i]);
      button.setAlignmentY(alignment);
      container.add(button);
    }
    return container;
  }

  public static void main(String args[]) {
    final JFrame frame = new JFrame("Alignment Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final Container panel1 = makeIt("Top", Component.TOP_ALIGNMENT);
   final Container panel2 = makeIt("Center", Component.CENTER_ALIGNMENT);
   final Container panel3 = makeIt("Bottom", Component.BOTTOM_ALIGNMENT);

    final Container contentPane = frame.getContentPane();
    contentPane.setLayout(new GridLayout(1, 2));
    contentPane.add(panel1);
    contentPane.add(panel2);
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {

		@Override
		public void run() {
			//contentPane.remove(panel1);
			// TODO Auto-generated method stub
			   contentPane.setLayout(new GridLayout(3, 1));
			    contentPane.add(panel3);
			    contentPane.setSize(600, 600);
			    contentPane.repaint();
			    frame.repaint();
			    contentPane.validate();
			    frame.validate();
		}
    	
    }, 3000);
 
    
    
   

    frame.setSize(423, 171);
    frame.setVisible(true);
  }
}